/*
 * Copyright (C) 2016 The Android Open Source Project
 *  Created by  Ant  2016/8/26  10:11
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ant.my.camera.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by baichuan on 2016/8/26.
 */
public class MyGui extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder holder;
    private boolean isDraw = false;
    private RenderThread thread;


    public MyGui(Context context) {
        super(context);
        holder = this.getHolder();
        holder.addCallback(this);
        thread = new RenderThread();

    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        isDraw = true;
        thread.start();

    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        isDraw = false;

    }


    class RenderThread extends Thread {
        @Override
        public void run() {
            while (isDraw) {
                drawGui();
            }
        }
    }


    private void drawGui() {
        Canvas canvas=holder.lockCanvas();
        try {
            antDraw(canvas);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            holder.unlockCanvasAndPost(canvas);
        }
    }

    private void antDraw(Canvas canvas) {
        Paint paint=new Paint();
        paint.setColor(Color.RED);
        canvas.drawLine(0,0,200,200,paint);

    }
}
